﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class XorderDetailDA : IXorderDetailDA
    {
        private string BASE_ORDER_ID = "@BASE_ORDER_ID";
        private string CONVERTED_QTY = "@CONVERTED_QTY";
        private string CREATED_BY = "@CREATED_BY";
        private string LINE_ID = "@LINE_ID";
        private string OPEN_QTY = "@OPEN_QTY";
        private string ORDER_ID = "@ORDER_ID";
        private string ORIGINAL_QTY = "@ORIGINAL_QTY";
        private string OWNER_ID = "@OWNER_ID";
        private string PROCESSED_QTY = "@PROCESSED_QTY";
        private string SEQUENCE = "@SEQUENCE";
        private string SHIPPED_QTY = "@SHIPPED_QTY";
        private string SKU_ID = "@SKU_ID";
        private string SQL_CHECK_XORDER_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@XORDER_DETAIL WHERE ORDER_ID = @ORDER_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_XORDER_DETAIL = " DELETE FROM @_@XORDER_DETAIL WHERE ORDER_ID = @ORDER_ID AND LINE_ID = @LINE_ID ";
        private string SQL_INSERT_XORDER_DETAIL = " INSERT INTO @_@XORDER_DETAIL ( WH_ID, ORDER_ID, LINE_ID, OWNER_ID, SKU_ID, ORIGINAL_QTY, SHIPPED_QTY, OPEN_QTY, PROCESSED_QTY, STATUS, TO_PROCESS_QTY, SEQUENCE, BASE_ORDER_ID, CONVERTED_QTY, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @ORDER_ID, @LINE_ID, @OWNER_ID, @SKU_ID, @ORIGINAL_QTY, @SHIPPED_QTY, @OPEN_QTY, @PROCESSED_QTY, @STATUS, @TO_PROCESS_QTY, @SEQUENCE, @BASE_ORDER_ID, @CONVERTED_QTY, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_XORDER_DETAIL = " SELECT  ROWID, WH_ID, ORDER_ID, LINE_ID, OWNER_ID, SKU_ID, ORIGINAL_QTY, SHIPPED_QTY, OPEN_QTY, PROCESSED_QTY, STATUS, TO_PROCESS_QTY, SEQUENCE, BASE_ORDER_ID, CONVERTED_QTY, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@XORDER_DETAIL ";
        private string SQL_UPDATE_XORDER_DETAIL = " UPDATE @_@XORDER_DETAIL SET WH_ID = @WH_ID, OWNER_ID = @OWNER_ID, SKU_ID = @SKU_ID, ORIGINAL_QTY = @ORIGINAL_QTY, SHIPPED_QTY = @SHIPPED_QTY, OPEN_QTY = @OPEN_QTY, PROCESSED_QTY = @PROCESSED_QTY, STATUS = @STATUS, TO_PROCESS_QTY = @TO_PROCESS_QTY, SEQUENCE = @SEQUENCE, BASE_ORDER_ID = @BASE_ORDER_ID, CONVERTED_QTY = @CONVERTED_QTY, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE ORDER_ID = @ORDER_ID AND LINE_ID = @LINE_ID  ";
        private string SQL_UPDATE_XORDER_DETAIL_STATUS_AND_QTY = "UPDATE @_@XORDER_DETAIL SET STATUS=@STATUS, SHIPPED_QTY = @SHIPPED_QTY, UPDATED_DATE=GETDATE(),UPDATED_BY=@UPDATED_BY WHERE ORDER_ID=@ORDER_ID AND LINE_ID=@LINE_ID";
        private string STATUS = "@STATUS";
        private string TO_PROCESS_QTY = "@TO_PROCESS_QTY";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckXorderDetailIDUnique(string xorderID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", xorderID), new SqlParameter("@LINE_ID", lineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_XORDER_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckXorderDetailIDUnique(DataBase dataBase, DbTransaction tran, string xorderID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", xorderID), new SqlParameter("@LINE_ID", lineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_XORDER_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteXorderDetail(string xorderID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < lineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", xorderID), new SqlParameter("@LINE_ID", lineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_XORDER_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteXorderDetail(DataBase dataBase, DbTransaction tran, string xorderID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < lineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", xorderID), new SqlParameter("@LINE_ID", lineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_XORDER_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllXorderDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XORDER_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllXorderDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XORDER_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public XorderDetailInfo GetXorderDetailByID(string xorderID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XORDER_DETAIL.Replace("@_@", whLoginID) + " WHERE ORDER_ID = @ORDER_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", xorderID), new SqlParameter("@LINE_ID", lineID) };
            XorderDetailInfo xorderDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    xorderDetailInfo = new XorderDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), (reader["ORIGINAL_QTY"].ToString() != "") ? decimal.Parse(reader["ORIGINAL_QTY"].ToString()) : 0M, (reader["SHIPPED_QTY"].ToString() != "") ? decimal.Parse(reader["SHIPPED_QTY"].ToString()) : 0M, (reader["OPEN_QTY"].ToString() != "") ? decimal.Parse(reader["OPEN_QTY"].ToString()) : 0M, (reader["PROCESSED_QTY"].ToString() != "") ? decimal.Parse(reader["PROCESSED_QTY"].ToString()) : 0M, reader["STATUS"].ToString(), (reader["TO_PROCESS_QTY"].ToString() != "") ? decimal.Parse(reader["TO_PROCESS_QTY"].ToString()) : 0M, (reader["SEQUENCE"].ToString() != "") ? decimal.Parse(reader["SEQUENCE"].ToString()) : 0M, reader["BASE_ORDER_ID"].ToString(), (reader["CONVERTED_QTY"].ToString() != "") ? decimal.Parse(reader["CONVERTED_QTY"].ToString()) : 0M, (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return xorderDetailInfo;
        }

        public XorderDetailInfo GetXorderDetailByID(DataBase dataBase, DbTransaction tran, string xorderID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XORDER_DETAIL.Replace("@_@", whLoginID) + " WHERE ORDER_ID = @ORDER_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", xorderID), new SqlParameter("@LINE_ID", lineID) };
            XorderDetailInfo xorderDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                xorderDetailInfo = new XorderDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), (reader["ORIGINAL_QTY"].ToString() != "") ? decimal.Parse(reader["ORIGINAL_QTY"].ToString()) : 0M, (reader["SHIPPED_QTY"].ToString() != "") ? decimal.Parse(reader["SHIPPED_QTY"].ToString()) : 0M, (reader["OPEN_QTY"].ToString() != "") ? decimal.Parse(reader["OPEN_QTY"].ToString()) : 0M, (reader["PROCESSED_QTY"].ToString() != "") ? decimal.Parse(reader["PROCESSED_QTY"].ToString()) : 0M, reader["STATUS"].ToString(), (reader["TO_PROCESS_QTY"].ToString() != "") ? decimal.Parse(reader["TO_PROCESS_QTY"].ToString()) : 0M, (reader["SEQUENCE"].ToString() != "") ? decimal.Parse(reader["SEQUENCE"].ToString()) : 0M, reader["BASE_ORDER_ID"].ToString(), (reader["CONVERTED_QTY"].ToString() != "") ? decimal.Parse(reader["CONVERTED_QTY"].ToString()) : 0M, (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return xorderDetailInfo;
        }

        public DataSet GetXorderDetailByQueryList(List<string> sqlWhere, XorderDetailQueryEntity xorderdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_XORDER_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!xorderdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, xorderdetailQuery.CurrentPage, xorderdetailQuery.PageSize, xorderdetailQuery.SortField, xorderdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetXorderDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, XorderDetailQueryEntity xorderdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_XORDER_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, xorderdetailQuery.CurrentPage, xorderdetailQuery.PageSize, xorderdetailQuery.SortField, xorderdetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<XorderDetailInfo> GetXorderDetailListByID(string xorderID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XORDER_DETAIL.Replace("@_@", whLoginID) + " WHERE ORDER_ID = @ORDER_ID   ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", xorderID) };
            List<XorderDetailInfo> XorderDetailList = new List<XorderDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    XorderDetailInfo xorderDetailInfo = new XorderDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), (reader["ORIGINAL_QTY"].ToString() != "") ? decimal.Parse(reader["ORIGINAL_QTY"].ToString()) : 0M, (reader["SHIPPED_QTY"].ToString() != "") ? decimal.Parse(reader["SHIPPED_QTY"].ToString()) : 0M, (reader["OPEN_QTY"].ToString() != "") ? decimal.Parse(reader["OPEN_QTY"].ToString()) : 0M, (reader["PROCESSED_QTY"].ToString() != "") ? decimal.Parse(reader["PROCESSED_QTY"].ToString()) : 0M, reader["STATUS"].ToString(), (reader["TO_PROCESS_QTY"].ToString() != "") ? decimal.Parse(reader["TO_PROCESS_QTY"].ToString()) : 0M, (reader["SEQUENCE"].ToString() != "") ? decimal.Parse(reader["SEQUENCE"].ToString()) : 0M, reader["BASE_ORDER_ID"].ToString(), (reader["CONVERTED_QTY"].ToString() != "") ? decimal.Parse(reader["CONVERTED_QTY"].ToString()) : 0M, (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    XorderDetailList.Add(xorderDetailInfo);
                }
            }
            return XorderDetailList;
        }

        public List<XorderDetailInfo> GetXorderDetailListByID(DataBase dataBase, DbTransaction tran, string xorderID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_XORDER_DETAIL.Replace("@_@", whLoginID) + " WHERE ORDER_ID = @ORDER_ID   ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", xorderID) };
            List<XorderDetailInfo> XorderDetailList = new List<XorderDetailInfo>();
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                XorderDetailInfo xorderDetailInfo = new XorderDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), (reader["ORIGINAL_QTY"].ToString() != "") ? decimal.Parse(reader["ORIGINAL_QTY"].ToString()) : 0M, (reader["SHIPPED_QTY"].ToString() != "") ? decimal.Parse(reader["SHIPPED_QTY"].ToString()) : 0M, (reader["OPEN_QTY"].ToString() != "") ? decimal.Parse(reader["OPEN_QTY"].ToString()) : 0M, (reader["PROCESSED_QTY"].ToString() != "") ? decimal.Parse(reader["PROCESSED_QTY"].ToString()) : 0M, reader["STATUS"].ToString(), (reader["TO_PROCESS_QTY"].ToString() != "") ? decimal.Parse(reader["TO_PROCESS_QTY"].ToString()) : 0M, (reader["SEQUENCE"].ToString() != "") ? decimal.Parse(reader["SEQUENCE"].ToString()) : 0M, reader["BASE_ORDER_ID"].ToString(), (reader["CONVERTED_QTY"].ToString() != "") ? decimal.Parse(reader["CONVERTED_QTY"].ToString()) : 0M, (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                XorderDetailList.Add(xorderDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return XorderDetailList;
        }

        public int InsertXorderDetail(XorderDetailInfo xorderDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                xorderDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_XorderDetail_Parameters(xorderDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_XORDER_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertXorderDetail(DataBase dataBase, DbTransaction tran, XorderDetailInfo xorderDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                xorderDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_XorderDetail_Parameters(xorderDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_XORDER_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_XorderDetail_Parameters(XorderDetailInfo xorderDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.ORDER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SKU_ID, SqlDbType.VarChar, 50), new SqlParameter(this.ORIGINAL_QTY, SqlDbType.Decimal, 13), new SqlParameter(this.SHIPPED_QTY, SqlDbType.Decimal, 13), new SqlParameter(this.OPEN_QTY, SqlDbType.Decimal, 13), new SqlParameter(this.PROCESSED_QTY, SqlDbType.Decimal, 13), new SqlParameter(this.STATUS, SqlDbType.VarChar, 20), new SqlParameter(this.TO_PROCESS_QTY, SqlDbType.Decimal, 13), new SqlParameter(this.SEQUENCE, SqlDbType.Decimal, 9), new SqlParameter(this.BASE_ORDER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CONVERTED_QTY, SqlDbType.Decimal, 13), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(xorderDetailInfo.WhID))
            {
                paramArray[0].Value = xorderDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = xorderDetailInfo.OrderID;
            paramArray[2].Value = xorderDetailInfo.LineID;
            paramArray[3].Value = xorderDetailInfo.OwnerID;
            paramArray[4].Value = xorderDetailInfo.SkuID;
            paramArray[5].Value = xorderDetailInfo.OriginalQty;
            paramArray[6].Value = xorderDetailInfo.ShippedQty;
            paramArray[7].Value = xorderDetailInfo.OpenQty;
            paramArray[8].Value = xorderDetailInfo.ProcessedQty;
            paramArray[9].Value = xorderDetailInfo.Status;
            paramArray[10].Value = xorderDetailInfo.ToProcessQty;
            paramArray[11].Value = xorderDetailInfo.Sequence;
            if (!string.IsNullOrEmpty(xorderDetailInfo.BaseOrderID))
            {
                paramArray[12].Value = xorderDetailInfo.BaseOrderID;
            }
            else
            {
                paramArray[12].Value = DBNull.Value;
            }
            paramArray[13].Value = xorderDetailInfo.ConvertedQty;
            paramArray[14].Value = xorderDetailInfo.CreatedBy;
            paramArray[15].Value = xorderDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateXorderDetail(XorderDetailInfo xorderDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                xorderDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_XorderDetail_Parameters(xorderDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_XORDER_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateXorderDetail(DataBase dataBase, DbTransaction tran, XorderDetailInfo xorderDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                xorderDetailInfo.WhID = whLoginID;
                SqlParameter[] paras = this.Set_XorderDetail_Parameters(xorderDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_XORDER_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateXorderDetailStatusAndQty(DataBase dataBase, DbTransaction tran, string orderID, string lineID, string status, decimal qty, string updateby, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@LINE_ID", lineID), new SqlParameter("@STATUS", status), new SqlParameter("@SHIPPED_QTY", qty), new SqlParameter("@UPDATED_BY", updateby) };
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_XORDER_DETAIL_STATUS_AND_QTY.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

